package com.elite.groovy.callback

import com.alibaba.fastjson.JSONObject
import com.elite.groovy.util.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class apiPushList1 implements ServiceBackInvoke {
	private static final Log log = LogFactory.getLog(apiPushList1.class);

	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");

		JSONObject result = new JSONObject();
		// 获取数据源List<Map<String, Object>>
		JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

		try {
			String selectSQL = "Select\n" +
					"  ob.objective_Guid,\n" +
					"  ob.Customer_Guid,\n" +
					"  c.Customername,\n" +
					"    case when T2.Mobile is not null  then T2.Mobile  \n" +
					"     when T2.tel_1  is not null then T2.tel_1 \n" +
					"     when T2.atel_1 is not null then T2.atel_1 \n" +
					"     when T2.atel_2 is not null then T2.atel_2  \n" +
					"     when T2.atel_3 is not null then T2.atel_3\n" +
					"     when T2.Atel_4 is not null then T2.atel_4  end  as mobile\n" +
					"From\n" +
					"  elitetask ob \n" +
					"  Left Join Customer T2 On ob.Customer_Guid = T2.Customer_Guid \n" +
					"  Left Join Tabccontacthis T3 On ob.objective_Guid = T3.Relation_Id \n" +
					"  Left Join Customer c On c.Customer_Guid = T2.Parent_Guid\n" +
					"  Left Join Custlists T9 On ob.Custlistname = T9.Custlist_Xxx\n" +
					"  Left Join Qt_Org T8 On T9.Custom2 = T8.Id And T8.Org_Level = '3' \n" +
					"  Left Join Wo3_Frame_Map wfm on wfm.Map_Id=ob.Map_Id\n" +
					"Where ob.obflag=1 \n" +
					" And  ob.Createddate> Add_Months(Sysdate, -6)\n" +
					"\n";
			List<Map<String, Object>> selectExits = jt.queryForList(selectSQL);

			if (selectExits.size() > 0) {
				result.put("code", 1);
				result.put("msg", "查询完成");
				result.put("data", selectExits );
			} else {
				result.put("code", 1);
				result.put("msg", "查询完成");
				result.put("data", selectExits);
			}

		} catch (Exception e) {
			log.error("接口处理异常", e);
			// 失败返回
			// LogToDB.toDB(eliteBean, dataSourceID, "api3VJIAAPIToken", "接口处理异常", "", "主程序异常", "--", e.getMessage());

			result.put("code", -1);
			result.put("msg", e.getMessage());
			result.put("data", "");

		}

		log.debug("推送名单处理完成返回结果:" + result.toString());

		return result.toString();
	}

}
